package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.db.dao.LitemallSeekHelpMapper;
import org.linlinjava.litemall.db.domain.LitemallSeekHelp;
import org.linlinjava.litemall.db.domain.LitemallSeekHelpExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class LitemallSeekHelpService {
    @Resource
    private LitemallSeekHelpMapper seekHelpMapper;

    //1新增一条.寻求帮助信息
    public void addSeekHelp(LitemallSeekHelp seekHelp){
        seekHelpMapper.insertSelective(seekHelp);
    }
    //2删除一条.
    public void delSeekHelp(Integer id){
        seekHelpMapper.deleteByPrimaryKey(id);
    }
    //3更改本条.
    public void updateSeekHelp(LitemallSeekHelp seekHelp){
        seekHelpMapper.updateByPrimaryKeySelective(seekHelp);
    }
    // @Sort @RequestParam(defaultValue = "add_time") String sort,@Order @RequestParam(defaultValue = "desc") String order
    //4查":所有的...求助内容
    public PageInfo seekHelpList(Map<String,String> searchMap, Integer page, Integer limit, String sort, String order){
        PageHelper.startPage(page,limit);
        LitemallSeekHelpExample example = new LitemallSeekHelpExample();
        LitemallSeekHelpExample.Criteria criteria = example.createCriteria();
        if (null != searchMap){
            //根据ID.ID 不能为空或""
            if(!StringUtils.isEmpty(searchMap.get("id"))){
                criteria.andIdEqualTo(Integer.parseInt(searchMap.get("id")));
            }
            //根据 title;//求助标题
            if(!StringUtils.isEmpty(searchMap.get("title"))){
                criteria.andTitleLike("%" +searchMap.get("title")+"%");
            }
            //根据 content;//求助内容
            if(!StringUtils.isEmpty(searchMap.get("content"))){
                criteria.andContentLike("%" +searchMap.get("content")+"%");
            }
            //根据 companyId;//公司ID
            if(!StringUtils.isEmpty(searchMap.get("companyId"))){
                criteria.andCompanyIdEqualTo(Integer.parseInt(searchMap.get("companyId")));
            }
            //根据 province;//省级
            if(!StringUtils.isEmpty(searchMap.get("province"))){
                criteria.andProvinceEqualTo(searchMap.get("province"));
            }
            //根据 userId;//用户ID
            if(!StringUtils.isEmpty(searchMap.get("userId"))){
                criteria.andUserIdEqualTo(Integer.parseInt(searchMap.get("userId")));
            }
            //根据 area;//那个领域
            if(!StringUtils.isEmpty(searchMap.get("area"))){
                criteria.andAreaLike("%" +searchMap.get("area") + "%" );
            }
            //根据 sample;//样品
            if(!StringUtils.isEmpty(searchMap.get("sample"))){
                criteria.andSampleEqualTo(searchMap.get("sample"));
            }

//    private String reply;//回复
//    private LocalDateTime addTime;//发布时间
//    private LocalDateTime replyTime;//回复时间
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

         List<LitemallSeekHelp> seekHelpList=seekHelpMapper.selectByExample(example);
        return  new PageInfo(seekHelpList);
    }


}
